﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Reflection;

namespace ZX.EHR.ResumeParseLib
{
    /// <summary>
    /// 执行解析类
    /// </summary>
    public static class ParseTools
    {
        /// <summary>
        /// 执行解析
        /// </summary>
        /// <param name="sourceType">与配置项中的Source一致</param>
        /// <param name="htmlString">待解析的HTML文本</param>
        /// <returns></returns>
        [Obsolete("请使用public static ResumeParsedInfo ExecuteParse(string sourceName, string htmlString)方法",false)]
        public static ResumeParsedInfo ExecuteParse(ResumeSouceType sourceType, string htmlString)
        {
            string sourceName = sourceType.ToString();
            return ExecuteParse(sourceName, htmlString);  
        }

        /// <summary>
        /// 执行解析
        /// </summary>
        /// <param name="sourceType">与配置项中的Source一致</param>
        /// <param name="htmlString">待解析的HTML文本</param>
        /// <returns></returns>
        public static ResumeParsedInfo ExecuteParse(string sourceName, string htmlString)
        {
            object o = ConfigurationManager.GetSection("resumeSourceConfig");
            ResumeSourceTypesConfig config = o as ResumeSourceTypesConfig;
            if (o == null)
                throw new ApplicationException("resumeSourceConfig配置错误");

            ResumeParsedInfo resumeInfo = null;            

            ResumeSourceTypeElement element = config.ResumeSourceTypes[sourceName];
            if (element == null)
                throw new ApplicationException(sourceName + "配置错误");

            string factoryName = element.ParsingFactory;
            string assemblyName = element.AssemblyName;

            if (string.IsNullOrEmpty(factoryName))
                throw new ApplicationException("Source配置错误");
            if (string.IsNullOrEmpty(assemblyName))
                throw new ApplicationException("AssemblyName配置错误");

            ParseFactory factory = (ParseFactory)Assembly.Load(assemblyName).CreateInstance(assemblyName + "." + factoryName);

            if (factory == null)
                throw new ApplicationException("Source和AssemblyName配置错误");

            resumeInfo = factory.Create().Parsing(htmlString);

            return resumeInfo;
        }         
    }
}
